<?php

/**
 * Payment form.
 *
 * @package    erp-crm
 * @subpackage form
 * @author     Milwell H. Sia
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class PaymentForm extends BasePaymentForm
{
  public function configure()
  {   
    if($this->isNew()){
      $this->validatorSchema['customer_iden'] = new sfValidatorString(array('required' => true));
      $this->validatorSchema['payment_iden'] = new sfValidatorString(array('required' => true));
      $this->setDefault('cash',0);
    }
    $this->widgetSchema['customer_iden'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['transaction_type'] = new sfWidgetFormInputHidden();
    
    $this->widgetSchema['payment_type'] = 
      new sfWidgetFormSelect(
        array('choices'=>ListDocumentTypesPeer::getList('PAYMENT'),)
      );
      
    $this->widgetSchema['counter_code'] = 
      new sfWidgetFormPropelChoice(
        array('model' => 'ListCounters', 'add_empty' => false, 'key_method'=>'getCode', )
      );
    
    $this->widgetSchema['cash']->setAttribute('size',10);
    $this->widgetSchema['payment_number']->setAttribute('size',15);

    $this->validatorSchema['remarks']->setOption('required',false);
    $this->validatorSchema['cash']->setOption('min',0.01);
    
    unset($this->widgetSchema['payment_invoice_list']);
    
    $this->widgetSchema->setLabels(
      array(
        'payment_type'=>'Payment#',
        'cash'=>'Cash',
        'remarks'=>'Remarks',
      )
    );
    
    $this->invoice_total = 0;
    
    $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(
        array(
          'callback'=>
             array(
               $this,'validateCash',
             ),
        )
      )
    );
  }
    
  public function setInvoiceTotal($i){
    $this->invoice_total = $i;
  }
  
  public function validateCash($validator,$values){
    if($this->isNew())
      return $values;
    $error = array();
    $invoice_total = PaymentPeer::getInvoiceTotal($values['payment_iden']);
    if (($values['cash']>0) && (($this->invoice_total+$invoice_total)==0)&&($this->isNew())){
      $error['cash'] = new sfValidatorError($validator,'Cash amount specified but invoice total is zero');
    }elseif($values['cash']<($this->invoice_total+$invoice_total)){
      $error['cash'] = new sfValidatorError($validator,'Cash amount should be more than equal total invoice amount');
    }
    if(count($error)>0)
      throw new sfValidatorErrorSchema($validator,$error);
    return $values;
  }
  
  public function setCashMinAllowZero(){
    $this->validatorSchema['cash']->setOption('min',0);
  }
    
  public function setNumberReadOnly(){
    $this->widgetSchema['payment_number']->setAttribute('readonly',true);
  }
  
  public function setNumberTypeReadOnly(){
    $this->widgetSchema['payment_type']->setAttribute('onFocus','this.blur();');
    $this->widgetSchema['payment_number']->setAttribute('readonly',true);
  }
  
}
